'this is a string'
'fire' + 'place'
'yo' * 2
'nan ' * 16 + 'batman!'
Write a function that wraps text in a banner of dashes.
For example,
'This statement would look better in a banner.'
becomes
'---------------------------------------------\nThis statement would look better in a banner.\n---------------------------------------------'
which prints as:
---------------------------------------------
This statement would look better in a banner.
---------------------------------------------
def banner(text):
"""Wrap the text in banner"""
num_dash = len(text)
return '-' * num_dash + '\n' + text + '\n' + '-' * num_dash
def banner(text):
"""Wrap the text in banner"""
size = len(text)
return '-' * size + '\n' + text + '\n' + '-' * size
result = banner('Go cougars!')
print(result)
result = banner('Cosmo is the best mascot in the world. Go cougars!')
print(result)
Modify the banner
function so you can specify what character to use in the banner.
def banner(text, banner_char):
"""Wrap the text in banner"""
num_dash = len(text)
return banner_char * num_dash + '\n' + text + '\n' + banner_char * num_dash
def banner(text, banner):
"""Wrap the text in banner"""
size = len(text)
return banner * size + '\n' + text + '\n' + banner * size
print(banner('This is epic', '*'))
print(banner('I got a job as a CS TA, and now I bring home bacon.', '$'))
print(banner('It snowed on the first day of summer!', '\u2603'))
for letter in 'this is a string':
print(letter)
is...
Methods¶'a'.isalpha(), '8'.isalpha()
'a'.isdigit(), '8'.isdigit()
'a'.isalnum(), '8'.isalnum()
'a'.isspace(), '8'.isspace(), ' '.isspace()
'a'.isalnum(), '8'.isalnum(), ' '.isalnum()
'a'.isspace(), '8'.isspace(), ' '.isspace()
characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*() \t\n'
# isalpha
keepers = []
for character in characters:
if character.isalpha():
keepers.append(character)
print(keepers)
# isdigit
keepers = []
for character in characters:
if character.isdigit():
keepers.append(character)
print(keepers)
# isspace
keepers = []
for character in characters:
if character.isspace():
keepers.append(character)
print(keepers)
Write a function that replaces every number in a string with ?
def no_numbers(text):
result = ''
for letter in text:
if letter.isdigit():
result = result + '?'
else:
result = result + letter
return result
def no_numbers(text):
result = ''
for char in text:
if char.isdigit():
result = result + '?'
else:
result = result + char
return result
no_numbers('There were 7 people.')
no_numbers('15 out of 25 have more than 17.3% contamination.')
no_numbers('2 + 2 = 5, for large values of 2.')
+=
¶message = 'Hello'
message = message + ' world!'
message
message = 'Hello'
message += ' world!'
message
Write a function that replaces all space characters with dashes.
def no_spaces(text):
result = ''
for letter in text:
if letter.isspace():
result += '-'
else:
result += letter
return result
def no_spaces(text):
result = ''
for c in text:
if c.isspace():
c = '-'
result += c
return result
print(no_spaces('BYU is the place to be.'))
message = """This is a long,
multiline string.
It has multiple lines.
That is what "multiline" means. :)"""
print(message)
print()
print(no_spaces(message))
print(no_spaces('Goodbye spaces \t tabs \n and newlines'))
'A'.islower(), 'A'.isupper()
'a'.islower(), 'a'.isupper()
'a'.upper(), 'a'.lower()
'A'.upper(), 'A'.lower()
in
¶'BYU' in 'I am a student at BYU.'
'BYU' in 'This room is full of monkeys!'
vowels = 'aeiou'
vowels += vowels.upper()
print(vowels)
found = ''
for letter in 'The Aeneid is ancient Greek literature.':
if letter in vowels:
found += letter
print(found)
vowels = 'aeiou'
print(vowels)
found = ''
for letter in 'The Aeneid is ancient Greek literature.':
if letter.lower() in vowels:
found = found + letter
print(found)
Capitalize every letter in an input string that is part of BYU
.
def byu(text):
"""Capitalize every letter of `text` that is part of 'BYU'"""
result = ''
for letter in text:
if letter in 'byu':
result += letter.upper()
else:
result += letter
return result
def byu(text):
"""Capitalize every letter of `text` that is part of 'BYU'"""
result = ''
for c in text:
if c in 'byu':
result += c.upper()
else:
result += c
return result
print(byu('write "byu" in all-caps'))
print(byu('Any student, boy or girl, young or old, can be a yodeler.'))
Add up all the digits found in a string.
def add_digits(text):
"""Add all the digits found in a `text`.
>>> add_digits('123foo')
6
"""
total = 0
for letter in text:
if letter.isdigit():
total += int(letter)
return total
def add_digits(text):
"""Add all the digits found in a `text`.
>>> add_digits('123foo')
6
"""
total = 0
for c in text:
if c.isdigit():
total += int(c)
return total
add_digits('123foo')
add_digits('10 students ate 6 oranges and 42 students ate 7 pears.')
'foo' + 'bar'
, 'BYU! ' * 5
.isalpha()
, .isdigit()
, .isalnum()
, .isspace()
, .isupper()
, .islower()
.upper()
, '.lower()
+=
in
, i.e. 'BYU' in 'my favorite school is BYU'